
--by Chris Mackey

include "attack.lua"
include "move.lua"

--pieces
--body
local head = piece "head"
local neck = piece "neck"
local torso = piece "torso"
local pelvis = piece "pelvis"
local kilt = piece "kilt"

--left side
local l_arm = piece "l_arm"
local l_fore = piece "l_fore"
local l_hand = piece "l_hand"
local l_thigh = piece "l_thigh"
local l_shin = piece "l_shin"
local l_foot = piece "l_foot"

--right side
local r_arm = piece "r_arm"
local r_fore = piece "r_fore"
local r_hand = piece "r_hand"
local r_thigh = piece "r_thigh"
local r_shin = piece "r_shin"
local r_foot = piece "r_foot"

local axe = piece "axe"
local s_sword = piece "s_sword"
local horns1 = piece "horns1"
local horns2 = piece "horns2"

--signals
local walk = 2
local aim  = 4

function script.Create()
	Hide( axe )
	Hide( horns1 )
end

function script.StartMoving()
	SetSignalMask( walk )
	StartThread( StopWalk )
	StartThread( Walk )
end

function script.StopMoving()
	Signal( walk )
	SetSignalMask( walk )
	StartThread( StopWalk )
end

function script.QueryWeapon1() return head end

function script.AimFromWeapon1() return head end

function script.AimWeapon1( heading, pitch )
	WaitForTurn( r_fore, x_axis )
	Signal( aim )
	SetSignalMask( aim )
	StartThread( Ready )
	WaitForTurn( r_hand, y_axis )
	return true
end

function script.FireWeapon1()
	StartThread( Attack )
end


local lostparts = {}

function script.HitByWeapon( x, z, weaponDefID, damage ) 
	EmitSfx( torso, 1024+1 )
	-- [[
	if WeaponDefs[weaponDefID].name == 'berserker_claymore' then
		local severity = 1 - math.random()
		local lose = {}
		if severity > 0.9 then
			lose = {l_hand, l_fore, l_arm, }
		elseif severity > 0.6 then
			lose = {l_hand, l_fore,}
		elseif severity > 0.4 then
			lose = {l_hand}
		end
		for _,part in ipairs(lose) do
			if not lostparts[part] then
				lostparts[part] = true
				Hide( part )
				Explode( part, SFX.FALL )
			end
		end
	end
	--]]
	--Spring.Echo (WeaponDefs[weaponDefID].name)
end

function script.Killed( damage, health )
	local severity = damage/health * 100
	
	if( severity <= 50 ) then
		EmitSfx( head, 1024+1 )
		EmitSfx( torso, 1024+1 )
		return 1 -- corpsetype
	elseif( severity <= 100 ) then
		EmitSfx( head,  1024+1 )
		EmitSfx( torso, 1024+1 )
		EmitSfx( r_arm, 1024+1 )
		EmitSfx( l_arm, 1024+1 )
		Explode( head, SFX.FALL )
		return 1 -- corpsetype
	else
		EmitSfx( torso, 1024+2 )
		Explode( head, SFX.FALL )
		Explode( torso, SFX.FALL )
		Explode( pelvis, SFX.FALL )
		Explode( l_arm, SFX.FALL )
		Explode( l_fore, SFX.FALL )
		Explode( l_thigh, SFX.FALL )
		Explode( l_shin, SFX.FALL )
		Explode( r_arm, SFX.FALL )
		Explode( r_fore, SFX.FALL )
		Explode( r_thigh, SFX.FALL )
		Explode( r_shin, SFX.FALL )
		return 2 -- corpsetype
	end
end
